package com.outsourcing.bottle.ui;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.outsourcing.bottle.BasicActivity;
import com.outsourcing.bottle.R;
import com.outsourcing.bottle.db.BottleTypeTable;
import com.outsourcing.bottle.db.LoginUserInfoTable;
import com.outsourcing.bottle.domain.BottleTypeEntry;
import com.outsourcing.bottle.domain.LoginUserInfoEntry;
import com.outsourcing.bottle.util.AppContext;
import com.outsourcing.bottle.util.BasicUIEvent;
import com.outsourcing.bottle.util.HttpUtils;
import com.outsourcing.bottle.util.ImageLoader;
import com.outsourcing.bottle.util.UICore;
/**
 * 
 * @author 06peng
 *
 */
public class BottleTipsActivity extends BasicActivity implements BasicUIEvent {
	
	private final int init_tips = 1;
	
	private int bottleId;
	private String gainbt_msg;
	private Bitmap avatarBitmap;
	
	private ImageView bottleIconView;
	private ImageView userAvatarView;
	private TextView tipsView;
	private Button gotitView;
	private ImageLoader btConfigLoader;
	private BottleTypeEntry entry;

	@Override
	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.bottle_tips);
		bottleId = getIntent().getIntExtra("bottleId", 0);
		gainbt_msg = getIntent().getStringExtra("gainbt_msg");
		initWidget();
		btConfigLoader = new ImageLoader(this, AppContext.BOTTLE_CONFIG);
		UICore.eventTask(this, this, init_tips, "init_tips", null);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return true;
		} else if (keyCode == KeyEvent.KEYCODE_HOME) {
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}
	
	@Override
	public void execute(int mes, Object obj) {
		switch (mes) {
		case init_tips:
			initTips();
			handler.sendEmptyMessage(init_tips);
			break;

		default:
			break;
		}
	}
	
	private void initWidget() {
		bottleIconView = (ImageView) findViewById(R.id.bottle_tips_image);
		userAvatarView = (ImageView) findViewById(R.id.bottle_tips_avatar);
		tipsView = (TextView) findViewById(R.id.bottle_tips);
		gotitView = (Button) findViewById(R.id.bottle_tips_gotit);
		gotitView.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(BottleTipsActivity.this, HomeActivity.class);
				intent.putExtra("need_refresh", true);
				startActivity(intent);
				BottleTipsActivity.this.finish();
			}
		});
	}
	
	private void initTips() {
		BottleTypeTable btt = new BottleTypeTable();
		entry = btt.getBottleType(bottleId);
		LoginUserInfoTable userTable = new LoginUserInfoTable();
		LoginUserInfoEntry userEntry = userTable.getLoginUserInfo(AppContext.getInstance().getLogin_uid());
		String avatar = userEntry.getAvatar();
		avatarBitmap = HttpUtils.getBitmapFromUrl(this, avatar);
	}
	
	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case init_tips:
				try {
					if (entry != null) {
						btConfigLoader.DisplayImage(entry.getBttype_sjwicon(), bottleIconView, R.drawable.bottle_type_1);
					}
					if (avatarBitmap != null) {
						userAvatarView.setImageBitmap(avatarBitmap);
					}
					tipsView.setText(gainbt_msg);
				} catch (Exception e) {
					e.printStackTrace();
				}
				break;

			default:
				break;
			}
		}
		
	};
}
